#include "fig_bullet.h"

#include "dd/dd_man.h"
#include "util/timer.h"
#include "math/facing.h"

FIGBullet::FIGBullet(	float Speed,
						bool Rotate,
						float DamagePower,
						BULLET_TYPES Type
					 ) : 
							m_IsAlive(false),
							m_Speed(Speed),
							m_bRotate(Rotate),
							m_Delay(0),
							m_DamagePower(DamagePower),
							m_Howlong(0),
							m_Type(Type)
{

}



void	FIGBullet::Render(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;	

	if(m_Delay > 0 )
		return;

	if(!m_Sprite.GetImage())
		return;

	m_Sprite.m_Pos = m_Pos + Offset;
	m_Sprite.m_Size = m_Size * Scale;


	m_Sprite.m_Rotation = m_Rotation;
	m_Sprite.RenderRotated();

	
}
	
void	FIGBullet::RenderDebug(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;	
    	
	dd::Manager&	DD = dd::Manager::Instance();
	DD.AddCircle2D( m_Pos+Offset, m_Radius*Scale, ColorRGBA(255,0,0) );
	
	
	math::Vec2 Dir = m_Dir;
	Dir.Rotate(180.0f);
	Dir *= m_Radius;

	for(int i = 0; i < (int)(m_Howlong/0.025f); i++)
	{
		DD.AddCircle2D( m_Pos + (Dir * (float)i) +Offset, m_Radius*Scale, ColorRGBA(255,0,0) );		
	}
	
}

void	FIGBullet::Tick(float dt)
{
	if(m_Delay > 0)
	{
		m_Delay -= dt;
		return;
	}

	m_Pos = m_Pos + ( m_Dir * m_Speed * dt);

	if( m_Pos.x < -0.2f || m_Pos.x > 1.2f || m_Pos.y < -0.05f  ||  m_Pos.y > 1.05f)
		m_IsAlive = false;

	
	if(m_bRotate)
	{
		m_Rotation  = ( m_Sprite.m_Rotation + (720 * dt));
		if(m_Rotation < 0)
			m_Rotation += 360;
		if(m_Rotation > 360)
			m_Rotation -= 360;
		
	}
	
	if(m_Sprite.GetImage())
		m_Sprite.Tick(dt);
	
	
}


void	FIGBullet::Clear()
{

}

void	FIGBullet::Load(const std::string& Name)
{
	m_Sprite.Create(Name);

	if( m_Type == FIGBullet::BULLET_TYPE_ENEMY_CIRCLE_DOUBLELAYER ||
		m_Type == FIGBullet::BULLET_TYPE_ENEMY_CIRCLE_GLOW ||
		m_Type == FIGBullet::BULLET_TYPE_ENEMY_CIRCLE_ROTATE ||
		m_Type == FIGBullet::BULLET_TYPE_PLAYER_GREEN ||
		m_Type == BULLET_TYPE_ENEMY_CIRCLE_TRIPPLELAYER)
	{
		m_Size = m_Sprite.m_Size * 1.25f;  // make it a little bigger
	}
	else if(
		m_Type == FIGBullet::BULLET_TYPE_PLAYER_BLUE ||		
		m_Type == FIGBullet::BULLET_TYPE_PLAYER_RED ) 	 		
	{
		m_Size = m_Sprite.m_Size * 1.5f;  // make it a little bigger
	}
	else
	{
		m_Size = m_Sprite.m_Size;
	}



	
	m_Radius = m_Size.x * 0.5f;  	
}


void	FIGBullet::ReSpawn(	const math::Vec2& Pos,
							FIGBullet::STARTPOS StartDir, 
							const math::Vec2& TargetDir
							)
{
    m_Rotation = math::Facing::AngleVector(TargetDir); 
	m_IsAlive = true;
	m_Pos = Pos;
	m_Sprite.m_Pos = math::Vec2(-1.0f,-1.0f);
	m_Dir = TargetDir;
     
}

std::string			FIGBullet::GetSpriteNameByType(FIGBullet::BULLET_TYPES Type)
{
	switch(Type)
	{
		case FIGBullet::BULLET_TYPE_PLAYER_BLUE:
			return "player_blue_laser";
		break;
		case FIGBullet::BULLET_TYPE_PLAYER_RED:
			return "gamma_ray";
		break;
		case FIGBullet::BULLET_TYPE_PLAYER_GREEN:
			return "Bullet1";
		break;

		case FIGBullet::BULLET_TYPE_PLAYER_ROCKET:
			return "rocket_0001";	
		break;

		case FIGBullet::BULLET_TYPE_ENEMY_CIRCLE_ROTATE:
			return "beta_ray";
		break;

		case FIGBullet::BULLET_TYPE_ENEMY_CIRCLE_GLOW:
			return "moving_alpha_ray";
		break;

		case BULLET_TYPE_ENEMY_CIRCLE_DOUBLELAYER:
			return "enemy_bullet_1";
		break;
		
		case BULLET_TYPE_ENEMY_CIRCLE_TRIPPLELAYER:
			return "enemy_bullet_2";
		break;
		
		
		case BULLET_TYPE_ENEMY_LONG_GREEN:
			return "enemy_green_laser";
		break;

		case BULLET_TYPE_ENEMY_LONG_RED:
			return "enemy_laser_red";
		break;

		case BULLET_TYPE_ENEMY_LONG_YELLOW:
			return "fire_lazer";
		break;

		case BULLET_TYPE_ENEMY_LONG_CYAN:
			return "enemy_laser_cyan";
		break;

		case FIGBullet::BULLET_TYPE_PLAYER_TOPIDO:
			return "followed_rocket";
		break; 

		

		default:
			break;
		
				
			
	}
	return "";
}

void		FIGBullet::Kill()
{ 
	m_IsAlive = false; 
}

float		FIGBullet::GetDamagePower() const
{   	
	return m_DamagePower;
}


